package me.ywy.kotlinvideo.presenter.impl

import android.os.Handler
import me.ywy.kotlinvideo.presenter.interf.IHomePresenter
import me.ywy.kotlinvideo.utils.URLProviderUtils
import me.ywy.kotlinvideo.view.HomeView
import okhttp3.*
import java.io.IOException

/**
 * Created by ywy on 2018-11-08.
 */
class HomePresenter(var homeView: HomeView) : IHomePresenter {

    var load: Boolean = false
    override fun loadMore(i: Int) {
        println("加载更多")

        Handler().postDelayed({
            if (load) {
                homeView.loadDataSauccess(i)
                load=!load
            } else {
                homeView.getDataFalse("加载失败")
                load=!load
            }
        }, 2000)


    }

    override fun loadDatas() {
        val path = URLProviderUtils.getHomeUrl(0, 20)
        val client = OkHttpClient();
        val request = Request.Builder()
                .url(path)
                .get()
                .build()
        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                println("获取数据成功" + Thread.currentThread().name)
                homeView.getDataSuccess()
            }

            override fun onFailure(call: Call, e: IOException) {
                println("获取数据失败" + Thread.currentThread().name)
                homeView.getDataFalse("加载失败")
            }

        })
    }

}